<?php
class Korelasi
{
	function Correlation($arr1, $arr2 )
		{       
			$correlation = 0;
		   
			$k = $this->SumProductMeanDeviation($arr1, $arr2);
			$ssmd1 = $this->SumSquareMeanDeviation($arr1);
			$ssmd2 = $this->SumSquareMeanDeviation($arr2);
		   
			$product = $ssmd1 * $ssmd2;
		   
			$res = sqrt($product);
		   
			$correlation = $k / $res;
		   
			return $correlation;
		}
	
		function SumProductMeanDeviation($arr1, $arr2)
		{
			$sum = 0;
		   
			$num = count($arr1);
		   
			for($i=0; $i<$num; $i++)
			{
				$sum = $sum + $this->ProductMeanDeviation($arr1, $arr2, $i);
			}
		   
			return $sum;
		}

		function ProductMeanDeviation($arr1, $arr2, $item)
		{
			return ($this->MeanDeviation($arr1, $item) * $this->MeanDeviation($arr2, $item));
		}

		function SumSquareMeanDeviation($arr)
		{
			$sum = 0;
		   
			$num = count($arr);
		   
			for($i=0; $i<$num; $i++)
			{
				$sum = $sum + $this->SquareMeanDeviation($arr, $i);
			}
		   
			return $sum;
		}

		function SquareMeanDeviation($arr, $item)
		{
			return $this->MeanDeviation($arr, $item) * $this->MeanDeviation($arr, $item);
		}

		function SumMeanDeviation($arr)
		{
			$sum = 0;
		   
			$num = count($arr);
		   
			for($i=0; $i<$num; $i++)
			{
				$sum = $sum + $this->MeanDeviation($arr, $i);
			}
		   
			return $sum;
		}

		function MeanDeviation($arr, $item)
		{
			$average = $this->Average($arr);
		   
			return $arr[$item] - $average;
		}   

		function Average($arr)
		{
			$sum = $this->Sum($arr);
			$num = count($arr);
		   
			return $sum/$num;
		}

		function Sum($arr)
		{
			return array_sum($arr);
		}
}